package com.xmall.pay.controller;

import com.xmall.annotation.LoginRequiredFalse;
import com.xmall.aspect.OperateRecord;
import com.xmall.define.base.Result;
import com.xmall.define.dict.OperateTypeEnum;
import com.xmall.define.dto.pay.AlipayRefundDto;
import com.xmall.define.dto.pay.AlipayRefundTradeDto;
import com.xmall.define.dto.pay.AlipayTradeDto;
import com.xmall.define.request.pay.AlipayRefundRequest;
import com.xmall.define.request.pay.AlipayRequest;
import com.xmall.pay.service.IAlipayService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RequestMapping("/service/pay/alipay")
@RestController
public class AlipayController {

    @Resource
    private IAlipayService alipayService;

    /**
     * 手机网站支付
     */
    @OperateRecord(type = OperateTypeEnum.UPDATE)
    @PostMapping("/mobileWapPay")
    public Result<String> mobileWapPay(@Validated @RequestBody AlipayRequest request) {
        return Result.success(alipayService.mobileWapPay(request));
    }

    /**
     * 交易查询
     */
    @LoginRequiredFalse
    @GetMapping("/tradeInfo")
    public Result<AlipayTradeDto> tradeInfo(@RequestParam("orderSn") String orderSn,
                                            @RequestParam(value = "aliTradeNo", required = false) String aliTradeNo) {
        return Result.success(alipayService.tradeInfo(orderSn, aliTradeNo));
    }

    /**
     * 退款查询
     */
    @LoginRequiredFalse
    @GetMapping("/refundTradeInfo")
    public Result<AlipayRefundTradeDto> refundTradeInfo(@RequestParam("orderSn") String orderSn,
                                                        @RequestParam(value = "aliTradeNo", required = false) String aliTradeNo,
                                                        @RequestParam("refundNo") String refundNo) {
        return Result.success(alipayService.refundTradeInfo(orderSn, aliTradeNo, refundNo));
    }

    /**
     * 退款
     */
    @OperateRecord(type = OperateTypeEnum.UPDATE)
    @LoginRequiredFalse
    @PostMapping("/refund")
    public Result<AlipayRefundDto> refund(@RequestBody AlipayRefundRequest request) {
        return Result.success(alipayService.refund(request));
    }

    /**
     * 支付回调
     */
    @OperateRecord(type = OperateTypeEnum.UPDATE)
    @LoginRequiredFalse
    @PostMapping("/tradeNotify")
    public String tradeNotify(HttpServletRequest request, HttpServletResponse response) {
        return alipayService.tradeNotify(request, response);
    }
}
